package views;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.example.zx.theline.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import Utils.DictionariesUtil;
import app.MyApplication;
import bean.SearchBean;
import greendao.UnitInformationDao;
import greendao.entity.UnitInformation;

/**
 * Created by Ren on 2017/8/3.
 */

public class ZsjhSearchView extends RelativeLayout {

    //类别
    Spinner spinner_lb;
    TextView lb;
    //线名
    TextView xm;
//    //开始日期
//    TextView ksrq;
//    //结束日期
//    TextView jsrq;
    //计划号
    TextView jhh;
    //单位和部门
    EditText dwhbm;
    //状态
    Spinner spinner_zt;
    TextView zt;

    TextView title_jhh;

    //状态 layout
    LinearLayout zt_layout;

    //确定
    TextView confirm;
    //取消
    TextView cancel;
    //清空
    TextView delect;


    OnClick mOnClick;
    SearchBean mSearchBean;
    List<UnitInformation> mSeleceUnitList = new ArrayList<>();
    ListPopWindow dwhbmPop;
    private List<UnitInformation> mAutoSource = new ArrayList<>();

    public ZsjhSearchView(Context context) {
        super(context);
        initView();
        initPickerView();
        initAutoCompleteTextView();
        initListener();
    }

    public ZsjhSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initPickerView();
        initAutoCompleteTextView();
        initListener();
    }

    public ZsjhSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initPickerView();
        initAutoCompleteTextView();
        initListener();

    }

    private void initView() {

        LayoutInflater.from(getContext()).inflate(R.layout.dialog_zsjhsearch, this);

        spinner_lb = (Spinner) findViewById(R.id.spinner_lb);
        lb = (TextView) findViewById(R.id.lb);
        xm = (TextView) findViewById(R.id.xm);
//        ksrq = (TextView) findViewById(R.id.ksrq);
//        jsrq = (TextView) findViewById(R.id.jsrq);
        jhh = (TextView) findViewById(R.id.jhh);
        dwhbm = (EditText) findViewById(R.id.dwhbm);
        zt = (TextView) findViewById(R.id.zt);
        spinner_zt = (Spinner) findViewById(R.id.spinner_zt);
        title_jhh = (TextView) findViewById(R.id.title_jhh);
        zt_layout = (LinearLayout) findViewById(R.id.zt_layout);

        confirm = (TextView) findViewById(R.id.confirm);
        cancel = (TextView) findViewById(R.id.cancel);
        delect = (TextView) findViewById(R.id.delect);
        if (dwhbmPop == null) {
            dwhbmPop = new ListPopWindow.Builder(getContext())
                    .setList(DictionariesUtil.getUnitList())
                    .setOutsideTouchable(false)
                    .setParentView(dwhbm)
                    .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                        @Override
                        public void OnListItemCilck(int position) {
                            dwhbm.setText(DictionariesUtil.getUnitList().get(position).toString());
                            dwhbmPop.dismiss();
                        }
                    }).builder();
        }

        mSearchBean=new SearchBean();
    }


    private void initListener() {

        spinner_lb.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                lb.setText(spinner_lb.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinner_zt.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                zt.setText(spinner_zt.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        //单位
        dwhbm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    return;
                } else {
                    if (!dwhbmPop.getWindow().isShowing()) {
                        dwhbmPop.show();
                    }
                    mAutoSource = MyApplication.getDaoSession()
                            .getUnitInformationDao()
                            .queryBuilder()
                            .where(UnitInformationDao.Properties.LjyyxDwxxName.like("%" + s + "%")).list();
                    if (mAutoSource.size() == 0) {
                        UnitInformation un = new UnitInformation();
                        un.setLjyyxDwxxName("没有匹配的单位");
                        mAutoSource.add(un);
                    }
                    dwhbmPop.setList(mAutoSource);
                }
            }
        });

        //确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!spinner_lb.getSelectedItem().toString().equals("全部")){
                    mSearchBean.lb = spinner_lb.getSelectedItem().toString();
                }
                mSearchBean.xm = xm.getText().toString();
                mSearchBean.jhh = jhh.getText().toString();
                mSearchBean.zt = zt.getText().toString();
                if (dwhbm.getText().length() != 0) {
                    List<UnitInformation> list = MyApplication.getDaoSession().getUnitInformationDao().queryBuilder()
                            .where(UnitInformationDao.Properties.LjyyxDwxxName.eq(dwhbm.getText().toString())).list();
                    if (list.size() != 0) {
                        mSearchBean.setDwhbmid(list.get(0).getLjyyxDwxxId());
                    } else {
                        //如果没有对应的单位 则设置搜索ID为-1
                        mSearchBean.setDwhbmid("-1");
                    }
                } else {
                    mSearchBean.setDwhbmid(null);
                }
                if (mOnClick != null) {
                    mOnClick.onConfirmListener(mSearchBean);
                }
                ZsjhSearchView.this.setVisibility(GONE);
            }
        });
        //取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ZsjhSearchView.this.setVisibility(GONE);
            }
        });
        //清空
        delect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                spinner_lb.setSelection(0);
                xm.setText("");
//                ksrq.setText("");
//                jsrq.setText("");
                jhh.setText("");
                dwhbm.setText("");
                zt.setText("");
                mSearchBean = new SearchBean();
            }
        });

    }

    //初始化AutoCompleteTextView
    private void initAutoCompleteTextView() {

    }

    private void initPickerView() {
    }

    public void setDefaultDate(SearchBean bean){
        if(mSearchBean==null){
            mSearchBean=bean;
        }
//        if(mSearchBean.getKsrq()!=null&&mSearchBean.getJsrq()!=null){
//            ksrq.setText(mSearchBean.getKsrq());
//            jsrq.setText(mSearchBean.getJsrq());
//        }

    }


    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public void setConfirmListener(OnClick onClick) {
        this.mOnClick = onClick;
    }

    public void hideZtlayout() {
        zt_layout.setVisibility(GONE);
    }


    public interface OnClick {
        void onConfirmListener(SearchBean Util);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }
}
